package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-07-29
 * Time: 21:11
 */
public class demo10 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            Thread currentThread = Thread.currentThread();//获取到线程的引用
            while (!currentThread.isInterrupted()) {//是否被终止
                System.out.println("Hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("执行到catch语句");
                    break;
                }
            }
            System.out.println("t 线程结束");
        });

        t.start();
        Thread.sleep(3000);
        t.interrupt();//在main线程中,终止t线程
    }
}
